import {
	createStore
} from 'vuex'
import httpURL, {
	storeName,
	pagesIndex,
	baseUrl
} from "@/config/index";
import {
	backPage
} from "@/utils/routes.js"

// Vue.use(Vuex)
// 初始化默认值 用于退出登录重置
const stateDefault = {
	baseUrl,
	httpURL,
	pagesIndex,
	storeName,
	loginFlag: false,
	userInfo: {},
	appConfig:{},
	token: ""
}
const stateData = uni.getStorageSync(storeName, {})
const store = createStore({
	state: {
		...stateDefault,
		...stateData

	},
	mutations: {
		init(state, data = {}) {
			if (data) {
				for (let k in state) {
					if (data[k]) {
						state[k] = data[k]
					} else if (stateDefault[k]) {
						state[k] = stateDefault[k]
					} else {
						delete state[k]
					}
				}
				uni.setStorageSync(storeName, state)
			}
		},
		setData(state, data = {}) {
			for (let k in data) {
				state[k] = data[k]
			}
			uni.setStorageSync(storeName, state)
		}
	},
	getters: {
		data(state) {
			return state
		}
	},
	actions: {
		async setData({
			commit
		}, data) {
			return new Promise((resolve) => {
				commit("setData", data)
				resolve(true)
			})
	}
}
})

export default store